import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
import cmath
import csvtest
import csv


def RecArray(L, a, dx,  showit = False):
    arrPos = []
    m = int(L/a)
    n = int(L/(a * np.sqrt(3) / 2))
    mty = -(a * np.sqrt(3) / 2) * int((L/2 - a * np.sqrt(3) / 4)/(a * np.sqrt(3) / 2))

    for i in range(n):
        #arrPos = list(arrPos)
        if i % 2 == 1:
            mk = m
            mtx = -dx - a * int((L / 2 - dx - a / 2) / a)
        else:
            mk = m-1
            mtx = -dx - a * int((L / 2 - dx - a / 2) / a) + a/2
        for j in range(mk):
            arrPos.append([mtx + a * j, mty + i * (a * np.sqrt(3) / 2)])

    arrPos = np.array(arrPos)
    if showit:
        print("number of elements: ", len(arrPos))
        plt.scatter(arrPos[:, 1], arrPos[:, 0], s=60, c='r')
        plt.plot(np.linspace(-L/2 - 0.01, L/2 + 0.01, 200), -dx * np.ones(200), '--')
        plt.axis('equal')
        plt.grid()
        plt.show()
    return arrPos

def RecArray_Circle(L, a, dx, r_cir, showit = False):
    arrPos = []
    m = int(L/a)
    n = int(L/(a * np.sqrt(3) / 2))
    mty = -(a * np.sqrt(3) / 2) * int((L/2 - a * np.sqrt(3) / 4)/(a * np.sqrt(3) / 2))
    for i in range(n):
        #arrPos = list(arrPos)
        if i % 2 == 1:
            mk = m
            mtx = -dx - a * int((L / 2 - dx - a / 2) / a)

        else:
            mk = m-1
            mtx = -dx - a * int((L / 2 - dx - a / 2) / a) + a/2

        for j in range(mk):
            xx = mtx + a * j
            yy = mty + i * (a * np.sqrt(3) / 2)
            if np.sqrt(xx*xx + yy*yy) < r_cir - a/2:
                arrPos.append([xx, yy])

    arrPos = np.array(arrPos)
    if showit:
        centerr = np.median(arrPos[:, 0])
        print(centerr)
        print("number of elements: ", len(arrPos))
        print("")
        plt.scatter(arrPos[:, 1], arrPos[:, 0], s=60, c='r')
        plt.plot(np.linspace(-L / 2 - 0.01, L / 2 + 0.01, 200), -dx * np.ones(200), '--')
        plt.scatter(0, centerr, s=100)
        plt.axis('equal')
        plt.grid()
        plt.show()
    return arrPos, round(centerr,6)

def RecArray_HexBoard(L, a, dx, showit = False):
    arrPos = []
    m = int(L/a)
    for i in range(int(m/2) + 1):
        if i == 0:
            mk = m
            mtx = -(mk-1)/2*a
            for j in range(mk):
                xx = mtx + a * j
                yy = 0
                arrPos.append([xx, yy])
        else:
            mk = m-i
            mtx = -(mk-1)/2*a

            for j in range(mk):
                xx = mtx + a * j
                yy1 = i * (a * np.sqrt(3) / 2)
                arrPos.append([xx, yy1])
                yy2 = - i * (a * np.sqrt(3) / 2)
                arrPos.append([xx, yy2])

    arrPos = np.array(arrPos)
    mdx = dx % 0.005
    if mdx > 0.0025:
        mdx = mdx - 0.005
    arrPos[:, 0] = arrPos[:, 0] - mdx
    centerr = np.median(arrPos[:, 0])

    if showit:
        print("number of elements: ", len(arrPos))
        plt.scatter(arrPos[:, 1], arrPos[:, 0], s=60, c='r')
        plt.plot(np.linspace(-L/2 - 0.01, L/2 + 0.01, 200), -dx * np.ones(200), '--')
        plt.scatter(0, centerr,  s=100)
        plt.axis('equal')
        plt.grid()
        plt.show()
    return arrPos, round(centerr,6)

def RecArray_Rec(L, a, dx,  showit = False):
    arrPos = []
    m = -round((dx % a) , 5)

    xx = np.arange(-L/2, L/2, a) + a / 2
    yy = np.arange(-L/2, L/2, a) + a / 2
    X, Y = np.meshgrid(xx, yy)

    X = np.array(np.append(X,[])) + m
    Y = np.array(np.append(Y, []))

    for i in range(len(X)):
        arrPos.append([X[i], Y[i]])
    arrPos = np.array(arrPos)
    if showit:
        print(m)
        print("number of elements: ", len(arrPos))
        plt.scatter(arrPos[:, 1], arrPos[:, 0], s=60, c='r')
        plt.plot(np.linspace(-L/2 , L/2 , 200), -dx * np.ones(200), '--')
        plt.axis('equal')
        plt.grid()
        plt.show()
    return arrPos

def RecArray_Rec_Circle(L, a, dx, r_cir, showit = False):
    arrPos0 = []
    m = -round((dx % a), 5)

    xx = np.arange(-L / 2, L / 2, a) + a / 2
    yy = np.arange(-L / 2, L / 2, a) + a / 2
    X, Y = np.meshgrid(xx, yy)

    X = np.array(np.append(X, [])) + m
    Y = np.array(np.append(Y, []))

    for i in range(len(X)):
        arrPos0.append([X[i], Y[i]])
    arrPos0 = np.array(arrPos0)

    arrPos = []
    for i in range(len(arrPos0)):
        xx, yy = arrPos0[i, 0]-m, arrPos0[i, 1]
        if np.sqrt(xx*xx + yy*yy) < r_cir - a/2:
                arrPos.append(arrPos0[i])
    arrPos = np.array(arrPos)

    if showit:
        centerr = np.median(arrPos[:, 0])
        # print(centerr)
        print("number of elements: ", len(arrPos))
        # print("")
        plt.scatter(arrPos[:, 1], arrPos[:, 0], s=60, c='r')
        plt.plot(np.linspace(-L / 2 - 0.01, L / 2 + 0.01, 200), -dx * np.ones(200), '--')
        plt.scatter(0, centerr, s=100)
        plt.axis('equal')
        plt.grid()
        plt.show()
    return arrPos

# 分贝转换
def k_dB(xt, r1020 = 20):
    if np.ndim(xt) == 0:
        return r1020*math.log10(xt)
    else:
        ann = np.zeros(len(xt))
        for i in range(len(xt)):
            ann[i] = r1020*math.log10(xt[i])
        return ann

def MaxD(f, a_all):
    c = 3e8  # m/s
    lam_0 = c / f
    D_max = 4 * math.pi * a_all / (lam_0 **2)
    D_max_dB = k_dB(D_max, 10)
    return D_max, D_max_dB

if __name__ == '__main__':
    L = 0.06
    print(MaxD(3.0*1e10, L*L*math.pi))
    aasd =1
    # RecArray_HexBoard(0.125, 0.005, 0.022, showit=True)